//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 		VisualNovelToolkit		/_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

namespace ViNoToolkit{

	[ CustomEditor( typeof(Scene))]
	[ System.Serializable ]
	public class SceneInspector : Editor {	
		public TextAsset sceneXmlFile;

		static public void DrawSceneNodeField( SceneData.SceneNodeData data , int offset ){
			float y = 20 + offset;
			GUI.Box( new Rect( 20 , y , 200 , 200 ) , "" );
			y += 20;
				data.name = EditorGUI.TextField( new Rect( 30 , y , 200 , 20 ) , "Name" , data.name );
			y += 20;
				data.parentname = EditorGUI.TextField( new Rect( 30 , y , 200 , 20 ) , "Parent" , data.parentname );
			y += 20;
			Vector3 pos = Vector3.zero;
			pos.x = data.posX;
			pos.y = data.posY;
			pos.z = data.posZ;
			pos = EditorGUI.Vector3Field( new Rect( 30 , y , 150 , 20 ) , "position" , pos );
			data.posX = pos.x;
			data.posY = pos.y;
			data.posZ = pos.z;
		}

		public override void OnInspectorGUI(){
			Scene targetData = target as Scene;
			
			DrawDefaultInspector();
									
			GUICommon.DrawLineSpace( 5f , 5f );
				ViNoEditorUtil.BeginGreenColor();
				if( GUILayout.Button( "Save as XML" ) ) {
					string xmlData =  ViNoGameSaveLoad.SerializeObject<SceneData.SceneNodeData[]>( targetData.sceneNodesData );
					ViNoGameSaveLoad.CreateXML( targetData.name + ".xml" , xmlData );

					AssetDatabase.Refresh();
					
					Debug.Log( string.Format( "Saved as {0} in project view : " , targetData.name + ".xml"  ) );
				}

				ViNoEditorUtil.EndGUIColor();

			EditorGUILayout.BeginHorizontal();

				sceneXmlFile = EditorGUILayout.ObjectField( sceneXmlFile , typeof( TextAsset ) , false ) as TextAsset;
				GUI.enabled = ( sceneXmlFile == null ) ? false : true;

				if( GUILayout.Button( "Load XML" ) ) {
					targetData.sceneNodesData =  ViNoGameSaveLoad.DeserializeObject<SceneData.SceneNodeData[]>( sceneXmlFile.text ) as SceneData.SceneNodeData[];
					EditorUtility.SetDirty( targetData );
				}

				GUI.enabled = true;
			
			EditorGUILayout.EndHorizontal();

				GUICommon.DrawLineSpace( 5f , 5f );

				if( ! Application.isPlaying ){
					GUI.enabled = false;			
					GUILayout.Label( "Save Scene : PlayMode Only. \n Save under the \"ADVScene\" objects data." );
				}

			EditorGUILayout.BeginHorizontal();
			
				if( GUILayout.Button( "Save Scene" , GUILayout.Height( 44f ) )){			
					if( EditorUtility.DisplayDialog( "Warning" , "Do you really want to overwrite ?" , "ok" , "cancel" ) ){
						ViNoSceneManager.FindInstance();
						ViNoSceneManager.Instance.SaveSceneNodes();
						targetData.sceneNodesData= ViNoSceneManager.Instance.GetSceneData().m_DataArray;
					}
				}
		
				GUI.enabled = true;					
			
				if( GUILayout.Button( "Load Scene" , GUILayout.Height( 44f ) )){				
					if( EditorUtility.DisplayDialog( "Warning" , "Do you really want to apply ?" , "ok" , "cancel" ) ){
						Undo.RegisterSceneUndo("Load Scene");
						SceneCreator.Create( targetData );
					}
				}

				GUILayout.Space( 20f );

			EditorGUILayout.EndHorizontal();			
		}

	}
}
